package com.itheima.reggie.controller;


import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.domain.Employee;
import com.itheima.reggie.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;


@RestController
public class EmployeeController {
    @Autowired
    private HttpSession session;
    @Autowired
    private EmployeeService employeeService;

    @PostMapping("/employee/login")
    public ResultInfo login(@RequestBody Map<String,String>map){
        String username = map.get("username");
        String password = map.get("password");
        ResultInfo resultInfo=employeeService.login(username,password);
        Employee employee = (Employee) resultInfo.getData();
        session.setAttribute("SESSION_NAME",employee);
        return resultInfo;
    }

    @PostMapping("/employee/logout")
    public ResultInfo logout(){
        session.invalidate();
        return ResultInfo.success(null);
    }
    @GetMapping("/employee/find")
    public ResultInfo findAll(String name){
        List<Employee> employeeList=employeeService.findAll(name);
        return ResultInfo.success(employeeList);
    }

    //保存
    @PostMapping("/employee")
    public ResultInfo save(@RequestBody Employee employee){
        employeeService.save(employee);
        return ResultInfo.success(null);
    }
    //根据id查询
    @GetMapping("/employee/{id}")
    public ResultInfo findById(@PathVariable("id") long id){
        Employee employee=employeeService.findById(id);
        return ResultInfo.success(employee);
    }
    //修改
    @PutMapping("/employee")
    public ResultInfo update(@RequestBody Employee employee){
        employeeService.update(employee);
        return ResultInfo.success(null);
    }
}
